package com.metaera.app.ui.adapters;

import android.content.res.Resources;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lxj.xpopup.XPopup;
import com.metaera.app.R;
import com.metaera.app.controller.presenter.LikeInformationPresenter;
import com.metaera.app.controller.view.BasicNetworkRequestView;
import com.metaera.app.data.model.ArticleListResponse;
import com.metaera.app.ui.popup.ShareCalendarPopup;
import com.metaera.app.ui.popup.ShareFlashNewsPopup;
import com.metaera.app.utils.DateUtils;
import com.metaera.app.utils.image.GlideApp;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;

public class FlashNewsCalendarItemAdapter extends BaseMultiItemQuickAdapter<ArticleListResponse, BaseViewHolder> {
    public FlashNewsCalendarItemAdapter() {
        addItemType(ArticleListResponse.CONTENT_TYPE, R.layout.item_calendar_child);
        addItemType(ArticleListResponse.DATE_TYPE, R.layout.item_flash_news_date);

        addChildClickViewIds(R.id.tv_share, R.id.tv_like_number, R.id.tv_deny_number);

        setOnItemChildClickListener((adapter, v, position) -> {
            if (v.getId() == R.id.tv_share) { // 分享快讯
                new XPopup.Builder(getContext())
                        .isLightStatusBar(false)
                        .asCustom(new ShareCalendarPopup(v.getContext(), getItem(position)))
                        .show();

            } else if (v.getId() == R.id.tv_like_number) { // 利好
                ArticleListResponse item = getItem(position);
                LikeInformationPresenter presenter = new LikeInformationPresenter();

                presenter.setModelAndView(new BasicNetworkRequestView<>(data -> {
                    item.setIs_like(item.getIs_like() == 1 ? 0 : 1); // 翻转, 是否利好状态
                    item.setIs_deny(item.getIs_like() == 1 ? 0 : 1);

                    // 增加利好数量
                    int currentLikeNum = item.getLike_num() + (item.getIs_like() == 1 ? 1 : -1);
                    item.setLike_num(Math.max(currentLikeNum, 0));
                    item.setDeny_num(item.getDeny_num() > 0 ? item.getDeny_num() - 1 : 0); // 增少利空数量

                    notifyItemChanged(position);
                }));

                presenter.like(item.getId(), item.getIs_like() == 0);

            } else if (v.getId() == R.id.tv_deny_number) { // 利空
                ArticleListResponse item = getItem(position);
                LikeInformationPresenter presenter = new LikeInformationPresenter();

                presenter.setModelAndView(new BasicNetworkRequestView<>(data -> {
                    item.setIs_deny(item.getIs_deny() == 1 ? 0 : 1); // 翻转, 是否利好状态
                    item.setIs_like(item.getIs_like() == 1 ? 0 : 1);

                    int currentLikeNum = item.getDeny_num() + (item.getIs_deny() == 1 ? 1 : -1);
                    item.setDeny_num(Math.max(currentLikeNum, 0));
                    item.setLike_num(item.getLike_num() > 0 ? item.getLike_num() - 1 : 0); // 增少利空数量

                    notifyItemChanged(position);
                }));

                presenter.deny(item.getId(), item.getIs_deny() == 0);
            }
        });

    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, ArticleListResponse item) {
        int timestamp = item.getRelease_time_stamp();

        if (item.getItemType() == ArticleListResponse.DATE_TYPE){
            holder.setText(R.id.tv_date, DateUtils.getTime(timestamp, DateUtils.PATTERN_6))
                    .setGone(R.id.tv_date_day, !DateUtils.siToday(timestamp))
                    .setText(R.id.tv_date_week, DateUtils.getWeekOfTimestamp(timestamp,
                            getContext().getResources().getStringArray(R.array.weeks)));
            return;
        }

        ImageView ivAvatar = holder.getView(R.id.iv_avatar);
        AppCompatTextView tvLikeNumber = holder.getView(R.id.tv_like_number);
        AppCompatTextView tvDenyNumber = holder.getView(R.id.tv_deny_number);
        Resources resources = ivAvatar.getContext().getResources();

        tvLikeNumber.setSelected(item.getIs_like() > 0);
        tvDenyNumber.setSelected(item.getIs_deny() > 0);

        RatingBar ratingBar = holder.getView(R.id.rating_bar);
        ratingBar.setRating(item.getScore());

        holder.setText(R.id.tv_title, item.getAuthor().getNickname())
                .setText(R.id.tv_des, item.getContent())
                .setText(R.id.tv_like_number, resources.getString(R.string.item_li_hao) + " " + item.getLike_num()) // 利好
                .setText(R.id.tv_deny_number, resources.getString(R.string.item_li_kong) + " " + item.getDeny_num()); // 利空

        GlideApp.with(ivAvatar).load(item.getAuthor().getAvatar_img()).avatar().into(ivAvatar);

    }


}
